﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DayPilot.Web.Ui.Events;
using DayPilot.Web.Ui.Events.Bubble;
using DayPilot.Web.Ui.Enums;
using System.Collections.Generic;
using System.Data.SqlClient;
using SiteManager;


public partial class includes_ucGoogleCalendae : System.Web.UI.UserControl
{
    private DataTable table;
    List<CalendarEvent> CalendarEventList = new List<CalendarEvent>();
    List<DentistCalendar> dentistCalendarEventList = new List<DentistCalendar>();
    
    protected void Page_Load(object sender, EventArgs e)
        
    
    {
       
        
        
       
        #region Data loading initialization
        if (Session["AllFeatures"] == null)
        {
                     
                if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    Session["dcotorId"] = Convert.ToString(Request.QueryString["Id"]);
                    string id = Convert.ToString(Session["dcotorId"]);
                    if (!String.IsNullOrEmpty(id))
                    {
                    CalendarEventList = CalenderRepository.GetCalendarEventsBYDrId(Convert.ToInt32(Session["dcotorId"]));
                    if (CalendarEventList.Count > 0)
                    {
                        DayPilotCalendar1.DataSource = CalendarEventList;
                        DayPilotNavigator1.DataSource = CalendarEventList;
                    }
                    }
                }

                else if (!String.IsNullOrEmpty(Request.QueryString["dentistId"]))
                {
                    Session["dentistId"] =Convert.ToString( Request.QueryString["dentistId"]);

                    dentistCalendarEventList = CalenderRepository.GetDentistEventsBYDentistId(Convert.ToInt32(Session["dentistId"]));
                    if (dentistCalendarEventList.Count > 0)
                    {
                        DayPilotCalendar1.DataSource = dentistCalendarEventList;
                        DayPilotNavigator1.DataSource = dentistCalendarEventList;
                    }

                }
            
            }
           
        ////if (Session["PatientId"] != null)
        ////{
        ////    DayPilotMenu1.Controls.RemoveAt(2);
        ////    ////DayPilotMenu1.FindControl("Delete");
        ////}
       
       
        
        #endregion

        if (!IsPostBack)
        {
            DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek();
            DataBind();
        }
    }

    protected void DayPilotCalendar1_EventMove(object sender, EventMoveEventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            con.Open();
            if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
            {
                SqlCommand cmd = new SqlCommand("UPDATE [CalendarEvent] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();
            }
            else
            {
                SqlCommand cmd = new SqlCommand("UPDATE [DentistCalendar] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();

            }

        }

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update();

        //#region Simulation of database update

        //DataRow dr = table.Rows.Find(e.Value);
        //if (dr != null)
        //{
        //    dr["start"] = e.NewStart;
        //    dr["end"] = e.NewEnd;
        //    table.AcceptChanges();
        //}

        //#endregion

        //DayPilotCalendar1.DataBind();

        //Hashtable ht = new Hashtable();
        //ht["message"] = "Event moved.";
        //ht["id"] = e.Value;

        //DayPilotCalendar1.Update(ht);
    }

    protected void DayPilotCalendar1_EventMenuClick(object sender, EventMenuClickEventArgs e)
    {
        if (e.Command == "Delete")
        {

            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                con.Open();
                if (Request.QueryString["Id"] != null)
                {
                SqlCommand cmd = new SqlCommand("DELETE FROM [CalendarEvent] WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.ExecuteNonQuery();
                CalendarEventList = CalenderRepository.GetCalendarEventsBYDrId(Convert.ToInt32(Session["dcotorId"]));
                DayPilotCalendar1.DataSource = CalendarEventList;
                     DayPilotCalendar1.DataBind();
                     DayPilotCalendar1.Update();
                }
                else
                {
                SqlCommand cmd = new SqlCommand("DELETE FROM [DentistCalendar] WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.ExecuteNonQuery();
                dentistCalendarEventList = CalenderRepository.GetDentistEventsBYDentistId(Convert.ToInt32(Session["dentistId"]));
                DayPilotCalendar1.DataSource = dentistCalendarEventList;
                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update();

                }

            }

           
        
        }
         else if(e.Command == "APPointmentDetail")
        {
            Session["eventId"] = e.Value;
            if (Request.QueryString["Id"] != null)
            {
                Response.Redirect("doctorAppointmentDetail.aspx");
            }
            else
            {

                Response.Redirect("DentistAppointmentDetail.aspx");
            }

        }
    }
    protected void DayPilotCalendar1_TimeRangeSelected(object sender, TimeRangeSelectedEventArgs e)
    {
        //if (Request.QueryString["Id"] != null)
        //{
            DayPilotCalendar1.EventClickHandling = DayPilot.Web.Ui.Enums.EventClickHandlingEnum.Disabled;
            DayPilotCalendar1.EventDeleteHandling = DayPilot.Web.Ui.Enums.UserActionHandling.Disabled;
            DayPilotCalendar1.EventDoubleClickHandling = DayPilot.Web.Ui.Enums.EventClickHandlingEnum.Disabled;
            DayPilotCalendar1.TimeRangeSelectedHandling = DayPilot.Web.Ui.Enums.TimeRangeSelectedHandling.Disabled;
        //}
       

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("New event created.");
    }

    protected void DayPilotCalendar1_EventResize(object sender, EventResizeEventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            con.Open();

            if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
            {
                SqlCommand cmd = new SqlCommand("UPDATE [CalendarEvent] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();


                CalendarEventList = CalenderRepository.GetCalendarEventsBYDrId(Convert.ToInt32(Session["dcotorId"]));
                DayPilotCalendar1.DataSource = CalendarEventList;
                DayPilotNavigator1.DataSource = CalendarEventList;

            }
            else
            {
                SqlCommand cmd = new SqlCommand("UPDATE [DentistCalendar] SET [eventstart] = @start, [eventend] = @end WHERE [id] = @id", con);
                cmd.Parameters.AddWithValue("id", e.Value);
                cmd.Parameters.AddWithValue("start", e.NewStart);
                cmd.Parameters.AddWithValue("end", e.NewEnd);
                cmd.ExecuteNonQuery();


                dentistCalendarEventList = CalenderRepository.GetDentistEventsBYDentistId(Convert.ToInt32(Session["dentistId"]));
                DayPilotCalendar1.DataSource = dentistCalendarEventList;
                DayPilotNavigator1.DataSource = dentistCalendarEventList;

            }

        }


        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("Event resized");
    }

    protected void DayPilotCalendar1_EventClick(object sender, EventClickEventArgs e)
    {
        Session["eventId"] = e.Value;
        if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
        {
            Response.Redirect("doctorAppointmentDetail.aspx");
            
        }
        else
        {
            Response.Redirect("DentistAppointmentDetail.aspx");
           
        }
    }
    protected void DayPilotCalendar1_EventEdit(object sender, EventEditEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["name"] = e.NewText;
            table.AcceptChanges();
        }

        #endregion

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("Event text changed.");
    }
    protected void DayPilotCalendar1_BeforeEventRender(object sender, BeforeEventRenderEventArgs e)
    {
        if (e.Value == "6")
        {
            e.BorderColor = "#1AAFE0";
            e.BackgroundColor = "#90D8F2";
        }
        if (e.Value == "8")
        {
            e.BorderColor = "#068c14";
            e.BackgroundColor = "#08b81b";
        }
        if (e.Value == "2")
        {
            e.BorderColor = "#990607";
            e.BackgroundColor = "#f60e13";
        }
    }


    protected void DayPilotCalendar1_EventDelete(object sender, EventDeleteEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            table.Rows.Remove(dr);
            table.AcceptChanges();
        }

        #endregion

        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update("Event deleted.");
    }

    protected void DayPilotCalendar1_EventSelect(object sender, DayPilotEventArgs e)
    {
        DayPilotCalendar1.Update();
    }

    protected void DayPilotCalendar1_Refresh(object sender, RefreshEventArgs e)
    {
        DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(e.StartDate);
        DayPilotCalendar1.DataBind();
        DayPilotCalendar1.Update(CallBackUpdateType.Full);
    }


    protected void DayPilotCalendar1_BeforeHeaderRender(object sender, BeforeHeaderRenderEventArgs e)
    {
        if (e.IsCorner)
        {
            e.InnerHTML = String.Format("<div style='padding:2px; text-align:center; font-weight: bold;'>{0}</div>", DayPilotCalendar1.StartDate.Year);
        }
    }

    protected void DayPilotBubble1_RenderContent(object sender, RenderEventArgs e)
    {
        //if (e is RenderEventBubbleEventArgs)
        //{
            //string html = @"<table width=""98%"" border=""0"">"+
            //                           @"<td align=""left"" class=""page-heading"" colspan=""3"" style=""height: 17px"">"+
            //                                    @"Please fill in the following information to create your Email ID and Password. To"+
            //                                    @"gain access to your profile, 4 out of the 5 fields must be verified."+
            //                                @"</td><tr><td align=""right"" style=""height: 1px; width: 150px;">
            //                                </td>
            //                                <td style="width: 3px; height: 1px">
            //                                </td>
            //                                <td style="height: 1px; width: 393px;" align="right">
            //                                    &nbsp;
            //                                </td>
            //                            </tr>
                                        
                                        
            //                            <tr>
            //                                <td align="left" colspan="3" style="height: 7px">
            //                                    &nbsp;
            //                                </td>
            //                            </tr>
                                       
            //                            <tr>
            //                                <td style="height: 17px; width: 150px;" align="right">
            //                                    When&nbsp;
            //                                </td>
            //                                <td style="width: 3px; height: 17px;">
            //                                    :
            //                                </td>
            //                                <td style="height: 17px; width: 393px;">
                                                
                                                    
            //                                        <asp:Label ID="lblWhen" CssClass="Labeltxt" runat="server" Text=""></asp:Label>
            //                                </td>
            //                            </tr>
            //                            <tr>
            //                            <td style="height: 20px; width: 150px;" align="right" colspan="3">
            //                            <hr size="1" style="color:#84BAD7" />
            //                            </td>
            //                            </tr>
            //                            <tr>
            //                                <td align="right" style="height: 17px; width: 150px;">
            //                                   With
            //                                </td>
            //                                <td style="width: 3px; height: 17px">
            //                                    :
            //                                </td>
            //                                <td style="height: 17px; width: 393px;">
                                                
            //                                    <asp:Label ID="lblWith" CssClass="Labeltxt" runat="server" Text=""></asp:Label>
            //                                </td>
            //                            </tr>
            //                            <tr>
            //                            <td style="height: 20px; width: 150px;" align="right" colspan="3">
            //                            <hr size="1" style="color:#84BAD7" />
            //                            </td>
            //                            </tr>
            //                            <tr>
            //                                <td align="right" style="height: 17px; width: 150px;">
            //                                 Where
            //                                </td>
            //                                <td style="width: 3px; height: 17px">
            //                                   :
            //                                </td>
            //                                 <td style="height: 17px; width: 393px;">
                                               
            //                                    @"<asp:Label ID=""lblWhere"" CssClass=""Labeltxt"" runat=""server"" Text=""></asp:Label></td></tr></table>";
           
            RenderEventBubbleEventArgs re = e as RenderEventBubbleEventArgs;
        //    re.InnerHTML = "<b>Event details</b><br />Here is the right place to show details about the event with ID: " + re.Value + ". This text is loaded dynamically from the server.<br/><b>Double-click the event to enter inline edit mode.</b>";
        //}
        //else if (e is RenderTimeBubbleEventArgs)
        //{
        //    RenderTimeBubbleEventArgs re = e as RenderTimeBubbleEventArgs;
        //    e.InnerHTML = "<b>Time header details</b><br />From:" + re.Start + "<br />To: " + re.End;
            
        //}
        //else if (e is RenderCellBubbleEventArgs)
        //{
        //    RenderCellBubbleEventArgs re = e as RenderCellBubbleEventArgs;
        //    e.InnerHTML = "<b>Cell details</b><br />Column:" + re.ResourceId + "<br />From:" + re.Start + "<br />To: " + re.End;
        //}
    }

    protected void DayPilotCalendar1_TimeRangeDoubleClick(object sender, TimeRangeDoubleClickEventArgs e)
    {
      ////  #region Simulation of database update
      ////DataRow dr = table.NewRow();
      ////  dr["start"] = e.Start;
      ////  dr["end"] = e.End;
      ////  dr["id"] = Guid.NewGuid().ToString();
      ////  dr["name"] = "New event";

      ////  table.Rows.Add(dr);
      ////  table.AcceptChanges();
      ////  #endregion

      ////  DayPilotCalendar1.DataBind();
      ////  DayPilotCalendar1.Update("New event created.");

    }

    protected void DayPilotCalendar1_TimeRangeMenuClick(object sender, TimeRangeMenuClickEventArgs e)
    {
        ////if (e.Command == "Insert")
        ////{
        ////    #region Simulation of database update

        ////    DataRow dr = table.NewRow();
        ////    dr["start"] = e.Start;
        ////    dr["end"] = e.End;
        ////    dr["id"] = Guid.NewGuid().ToString();
        ////    dr["name"] = "New event";

        ////    table.Rows.Add(dr);
        ////    table.AcceptChanges();
        ////    #endregion

        ////    DayPilotCalendar1.DataBind();
        ////    DayPilotCalendar1.Update();

        //}

    }
    protected void DayPilotCalendar1_Command(object sender, DayPilot.Web.Ui.Events.CommandEventArgs e)
    {
        switch (e.Command)
        {
            case "previous":
                DayPilotCalendar1.StartDate = DayPilotCalendar1.StartDate.AddDays(-7);
                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update(CallBackUpdateType.Full);
                break;
            case "navigate":
                DateTime start = (DateTime)e.Data["start"];
                DateTime end = (DateTime)e.Data["end"];

                DayPilotCalendar1.StartDate = start;
                //DayPilotCalendar1.Days = (int) (end - start).TotalDays;

                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update(CallBackUpdateType.Full);
                break;
            case "refresh":
                DayPilotCalendar1.DataBind();
                DayPilotCalendar1.Update();
                break;
        }


    }
    protected void DayPilotNavigator1_VisibleRangeChanged(object sender, EventArgs e)
    {
        DayPilotNavigator1.DataBind();
    }
    protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
    {
        if (e.Start >= DateTime.Today && e.End <= DateTime.Today.AddDays(1))
        {
            e.BackgroundColor = "#FFFFCE";
        }
       
    }
    //protected void btnBackToSearch_Click(object sender, EventArgs e)
    //{
    //    if (Request.QueryString["Id"] != null)
    //    {
    //        Response.Redirect("~/Doctor/doctorContent.aspx");
    //    }
    //    else
    //    {
    //        Response.Redirect("~/Dentist/DentistContent.aspx");

    //    }
    //}
}
